<template>
	<view class="page search-page">
		<view class="like-content bac-white" v-for="item in searchList" :key="item.id" @click="goDetail(item.id)">
			<image :src="item.thumb" class="like-image"></image>
			<view class="movie-desc">
				<view class="title">{{item.title}}</view>
				<uni-rate :value="item.score/2" size="14" margin="0" :disabled="true"></uni-rate>
				<view class="tag">{{item.basicInfo}}</view>
				<view class="tag">{{item.releaseDate}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				payload: {},
				page: 1,
				total: 1,
				limit: 10,
				searchList: []
			}
		},
		onNavigationBarButtonTap() {
			uni.navigateBack();
		},
		onNavigationBarSearchInputChanged (val) {
			this.payload.keywords = val.text;
			this.getSearch(true);
		},
		onLoad(e) {
			uni.showLoading({
				title: '加载中'
			})
			this.payload = {
				keywords: e.keywords,
				category_id: e.category_id
			}
			
			// #ifdef APP-PLUS  
			var webView = this.$mp.page.$getAppWebview();  
			// 设置 searchInput的 text  
			webView.setTitleNViewSearchInputText(this.payload.keywords)
			// #endif
			
			this.getSearch()
		},
		onReachBottom() {
			this.page++
			console.log(this.total, this.searchList.length, 5555)
			if (this.total <= this.searchList.length) {
				uni.showToast({
					title: '已加载全部',
					duration: 500
				})
			} else {
				console.log('翻页')
				this.getSearch()
			}

		},
		methods: {
			// 搜索
			search(e, val) {
				// 清空原数据
				this.searchList = []
				//this.keywords = e
				this.page = 1
				this.getSearch()
			},
			// 获取猜你喜欢
			getSearch(search) {
				uni.showLoading({
					title: '加载中'
				})
				let params = {
					keywords: this.payload.keywords,
					category_id: this.payload.category_id,
					page: this.page,
					limit: this.limit
				}
				uni.request({
					url: this.$host + '/courses',
					data: params,
					success: (res) => {
						let data = res.data.data
						//console.log(data)

						this.searchList = search ? data : this.searchList.concat(data)
						this.total = res.records
						let timer = null
						if (timer) {
							clearTimeout(timer)
						}
						timer = setTimeout(function() {
							uni.hideLoading()
						}, 500)
					}
				});
			},
			// 跳转详情页
			goDetail(id) {
				uni.navigateTo({
					url: './detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-page {
		.hot {
			.wrapper {
				display: flex;
				align-items: center;
				justify-items: center;
				padding: 15upx;

				.icon-like {
					width: 45upx;
					height: 45upx;
					padding-top: 5upx;
					padding-left: 5upx;
				}

				.title {
					font-size: 35upx;
					margin-left: 15upx;
					color: #3F51B5;
				}
			}
		}

		.search-movie {
			width: 100%;
		}

		.like-content {
			display: flex;
			padding: 30upx 50upx;

			.like-image {
				height: 180upx;
				width: 240upx;
				border-radius: 3%;
			}

			.movie-desc {
				flex: 1;
				padding: 0upx 45upx;
				line-height: 45upx;

				.title {
					font-size: 32upx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}

				.tag {
					font-size: 26upx;
					color: #808080;
				}
			}
		}
	}
</style>
